#include "backupsystem.h"

BackupSystem::BackupSystem()
{
    this->path = "";
}

#if defined( Q_OS_WIN32)
bool BackupSystem::save(QString command,QString fileName,QByteArray &output){
    QProcess cmd;
    if(this->path!=""){
        cmd.setWorkingDirectory(this->path);
        command = ".\\"+command;
    }
    output="";
    cmd.start("cmd");
    if (!cmd.waitForStarted())
        return false;
    command=command+">"+"\""+fileName+"\""+"\n";

    qDebug() << command;

    cmd.write(command.toAscii());
    cmd.closeWriteChannel();
    if (!cmd.waitForFinished())
        return false;
    QString err = cmd.readAllStandardError();

    qDebug() << err;

    if(err!="")
        return false;
    return true;

}
#else

bool BackupSystem::save(QString command,QString fileName,QByteArray &output){

    QProcess cmd;
    QStringList env = QProcess::systemEnvironment();

    for (int i = 0; i < env.size(); i++) {
      QString envVar = env.at(i);
      if (envVar.startsWith("PATH="))
        env.replace(i, envVar += ":/opt/local/bin/");
    }

    cmd.setEnvironment(env);


    qDebug() << env;
    output="";
    if(this->path!=""){
        cmd.setWorkingDirectory(this->path);
        command = "./"+command;
    }
    cmd.start(command);

    if (!cmd.waitForStarted())
        return false;
    /*cmd.write(QString("\\. "+fileName).toAscii());
    cmd.closeWriteChannel();*/

    if (!cmd.waitForFinished())
        return false;

     output = cmd.readAll();
    QFile file(fileName);
    if ( file.open( QIODevice::WriteOnly | QIODevice::Text) )
    {
        file.write(output.data());
        file.close();
    }
    return true;
}
#endif
/*
void Backend::saveTestFile(QString str){
    QFile file("salida.txt");
    file.open(QFile::Append);
    QTextStream out(&file);
     out << str;
     file.close();
}*/

#if defined(Q_OS_WIN32)
bool BackupSystem::load(QString command,QString fileName,QByteArray &output){
    QProcess cmd;
    output="";
    if(this->path!=""){
        cmd.setWorkingDirectory(this->path);
        command = ".\\"+command;
    }
    cmd.start("cmd");
    if (!cmd.waitForStarted())
        return false;
    command = command + "<"+"\""+fileName+"\""+"\n";
    cmd.write(command.toAscii());
    cmd.closeWriteChannel();
    if (!cmd.waitForFinished())
        return false;
    output = cmd.readAll();
    if(cmd.readAllStandardError()!="")
        return false;
    return true;
}
#else
bool BackupSystem::load(QString command,QString fileName,QByteArray &output){
    QProcess cmd;
    output="";
    if(this->path!=""){
        cmd.setWorkingDirectory(this->path);
        command = "./"+command;
    }
    cmd.start(command);
    if (!cmd.waitForStarted())
        return false;
    cmd.write(QString("\\. "+fileName).toAscii());
    cmd.closeWriteChannel();
    if (!cmd.waitForFinished())
        return false;
    output = cmd.readAll();
    return true;
}
#endif


void BackupSystem::setPath(QString path){
    this->path = path;
    if(this->path=="") return;
    #if defined(Q_OS_WIN32)
        this->path = path + "\\";
    #else
        this->path = path + "//";
    #endif
}

bool BackupSystem::makeBackup(QString fileName,QString dbName,QString user,QString pass){
    QByteArray output="";
    QString command ="mysqldump --opt --password="+pass+" --user="+user+" "+ dbName;
    return this->save(command,fileName,output);
}

bool BackupSystem::loadBackup(QString fileName,QString dbName,QString user,QString pass){
    QByteArray output="";
    QString command = "mysql --password="+pass+" --user="+user+" "+ dbName;
    return this->load(command,fileName,output);
}
